﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class VSRBase
    {
        public VisualScript_Node Node;

        protected Rectangle SmallerRectangle;
        protected Rectangle BiggerRectangle;

        public Vector2 Position;

        public Vector2 Size;

        public Camera2D Camera;

        public VisualScriptEditor Editor;

        public RectangleOwner LeftRectangle;
        public RectangleOwner RightRectangle;

        public VSRBase()
        {
            Size = new Vector2(150, 60);
            SmallerRectangle = new Rectangle();
            BiggerRectangle = new Rectangle();
        }
        public virtual void Start(VisualScript_Node Node, VisualScriptEditor editor)
        {
            this.Node = Node;
            this.Editor = editor;

            this.LeftRectangle = new RectangleOwner(this, new Rectangle(), this.Node.Conditionals, ExpectedNode.Condition);
            editor.Rectangles.Add(LeftRectangle);
            this.RightRectangle = new RectangleOwner(this, new Rectangle(), null, ExpectedNode.Condition);
            editor.Rectangles.Add(RightRectangle);

            UpdateSizeAndPos();
        }

        protected int LineSize = 2;
        protected int RightRectSize = 26;
        protected int LeftRectWidth = 13;
        protected int LeftRectHeight = 26;

        protected void UpdateRightRectangle()
        {
            RightRectangle.Rectangle.X = (int)Position.X + (int)Size.X - RightRectSize;
            RightRectangle.Rectangle.Y = (int)Position.Y + (((int)Size.Y / 2) - RightRectSize / 2);
            RightRectangle.Rectangle.Width = RightRectSize;
            RightRectangle.Rectangle.Height = RightRectSize;
        }
        protected void UpdateLeftRectangle()
        {
            LeftRectangle.Rectangle.X = (int)Position.X;
            LeftRectangle.Rectangle.Y = (int)Position.Y + (((int)Size.Y / 2) - RightRectSize / 2);
            LeftRectangle.Rectangle.Width = LeftRectWidth;
            LeftRectangle.Rectangle.Height = LeftRectHeight;
        }

        protected void UpdateSquareRectangles()
        {
            SmallerRectangle.X = (int)Position.X;
            SmallerRectangle.Y = (int)Position.Y;
            SmallerRectangle.Width = (int)Size.X;
            SmallerRectangle.Height = (int)Size.Y;
            BiggerRectangle.X = (int)Position.X - LineSize;
            BiggerRectangle.Y = (int)Position.Y - LineSize;
            BiggerRectangle.Width = (int)Size.X + (LineSize * 2);
            BiggerRectangle.Height = (int)Size.Y + (LineSize * 2);
        }

        public virtual void UpdateSizeAndPos()
        {
            UpdateRightRectangle();

            UpdateLeftRectangle();

            UpdateSquareRectangles();
        }

        private bool Moving;
        public virtual void Update()
        {
            UpdateSizeAndPos();

            if (Engine.Input.MouseLeftDown())
            {
                Rectangle rec = Engine.Input.MouseRect(1);
                if (rec.Intersects(this.BiggerRectangle))
                {
                    if (Editor.SetProp != null)
                        Editor.SetProp(this.Node);
                }
            }
            if (!Moving)
            {
                Rectangle pos = Engine.Input.MouseRect();
                if (pos.Intersects(this.BiggerRectangle) &&
                    Engine.Input.MouseRightClick())
                {
                    if (Editor.MovingNode == null)
                    {
                        Editor.MovingNode = this;
                        Moving = true;
                    }
                }
            }
            else
            {
                if (Engine.Input.MouseRightUp())
                {
                    Moving = false;
                    Editor.MovingNode = null;
                }
                this.Position = Engine.Input.MousePos();
            }
        }

        public virtual void Draw()
        {
            Engine.RenderManager.DrawRectangle(Color.Black, BiggerRectangle, Camera);
            Engine.RenderManager.DrawRectangle(Color.Gray, SmallerRectangle, Camera);
            Engine.RenderManager.DrawString(Node.DescriptionName, 2, 200,
                new Vector2(Position.X + Size.X / 10, Position.Y + Size.Y / 10), "Kootenay",
                Color.White, Camera, 0, Vector2.Zero, 0.4f, 0);

            LeftRectangle.Draw(Camera);
            RightRectangle.Draw(Camera);

            for (int i = 0; i < Node.Conditionals.Count; i++)
            {
                Rectangle first = LeftRectangle.Rectangle;
                Rectangle second = Node.Conditionals[i].Representation.RightRectangle.Rectangle;
                Engine.RenderManager.Draw2DLine(new Vector2(first.X + (first.Width / 2),
                    first.Y + (first.Height / 2)),
                    new Vector2(second.X + (second.Width / 2),
                        second.Y + (second.Height / 2))
                        , Camera, Color.Aqua, 5);
            }
        }

        public virtual void SerializeData(SerializeData Data)
        {
            Data.AddData(Node.CreatedNumber + "Representation.Position", this.Position);
        }
        public virtual void DeserializeData(SerializeData Data)
        {
            this.Position = Data.GetData<Vector2>(Node.CreatedNumber + "Representation.Position");
        }
    }
}
